package com.lancoo.ccas53.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lancoo.ccas53.entity.*;
import com.lancoo.ccas53.pojo.common.PageInfo;
import com.lancoo.ccas53.pojo.vo.*;
import com.lancoo.ccas53.service.*;
import com.lancoo.ccas53.util.PagingUtil;
import com.lancoo.ccas53.util.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName BasicDataController
 * @Description 基础信息
 * @Author quin
 * @Date 2024/5/22 9:54
 */
@Api(tags = "基础信息")
@RestController
@RequestMapping("/basicData")
public class BasicDataController {
    @Resource
    private RoomTypeService roomTypeService;
    @Resource
    private RuleWeekDayService ruleWeekDayService;
    @Resource
    private ActiveTeachService activeTeachService;
    @Resource
    private HolidayService holidayService;
    @Resource
    private RoomService roomService;
    @Resource
    private CollegeService collegeService;
    @Resource
    private MajorGradeService majorGradeService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private StudentService studentService;


    @GetMapping("/getPeriodSchemes")
    @ApiOperation("获取课时方案")
    public ResponseObject<List<PeriodSchemeGroupVo>> getPeriodSchemes(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(ruleWeekDayService.getPeriodSchemes(taskId));
    }

    @GetMapping("/getWeekDays")
    @ApiOperation("获取课时方案设置")
    public ResponseObject<List<RuleWeekDay>> getWeekDays(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(ruleWeekDayService.getWeekDays(taskId));
    }

    @GetMapping("/getMaxPeriodScheme")
    @ApiOperation("获取最大课时方案")
    public ResponseObject<MaxPeriodScheme> getMaxPeriodScheme(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(ruleWeekDayService.getMaxPeriodScheme(taskId));
    }

    @PostMapping("/updateWeekDays")
    @ApiOperation("设置课时方案")
    public ResponseObject<Boolean> updateWeekDay(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                 @ApiParam(value = "课时请求参数 ", required = true) @RequestBody List<RuleWeekDay> ruleWeekDays) {
        return ResponseObject.ofSuccess(ruleWeekDayService.updateWeekDay(taskId, ruleWeekDays));
    }

//    @PostMapping("/copyWeekDays")
//    @ApiOperation("复制同学期设置-课时方案")
//    public ResponseObject<Boolean> copyWeekDays(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
//        return ResponseObject.ofSuccess(ruleWeekDayService.copyWeekDays(taskId));
//    }


//    @GetMapping("/getProhibit")
//    @ApiOperation("获取禁排")
//    public Result<List<RuleTimeProhibit>> getTimeProhibit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
//                                                          @ApiParam(value = "1、日期禁排 2、场地禁排 3、课程禁排优先排 4、教师禁排优先排", required = true) @RequestParam Integer optionType,
//                                                          @ApiParam(value = "教室id/教学班id/教师id ", required = false) @RequestParam(required = false) Integer optionId) {
//        return Result.ofSuccess(ruleWeekDayService.getTimeProhibit(taskId, optionType, optionId));
//    }
//
//    @PostMapping("/setProhibit")
//    @ApiOperation("设置禁排")
//    public Result<Boolean> setTimeProhibit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
//                                           @ApiParam(value = "1、日期禁排 2、场地禁排 3、课程禁排优先排 4、教师禁排优先排", required = true) @RequestParam Integer optionType,
//                                           @ApiParam(value = "教室id/教学班id/教师id ", required = false) @RequestParam(required = false) String optionId,
//                                           @ApiParam(value = "日期禁排实体类 ", required = true) @RequestBody List<RuleTimeProhibit> ruleTimeProhibits) {
//        return Result.ofSuccess(ruleWeekDayService.setTimeProhibit(taskId, optionType, optionId, ruleTimeProhibits));
//    }

    @GetMapping("/getMajorGradeActiveTeachList")
    @ApiOperation("获取教学活动安排列表")
    public ResponseObject<PageInfo<MajorGrade>> getMajorGradeActiveTeachList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                             @ApiParam(value = "基础平台学院id", required = false) @RequestParam(required = false) String baseCollegeId,
                                                                             @ApiParam(value = "对应年级id", required = false) @RequestParam(required = false) String gradeId,
                                                                             @ApiParam(value = "基础平台专业id", required = false) @RequestParam(required = false) String baseMajorId,
                                                                             @ApiParam(value = "专业模糊查询", required = false) @RequestParam(required = false) String keyword,
                                                                             @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                             @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<MajorGrade> page = new Page<>(currentPage, pageSize);
        return ResponseObject.ofSuccess(ruleWeekDayService.getMajorGradeActiveTeachList(page, taskId, baseCollegeId, gradeId, baseMajorId, keyword));
    }

    @GetMapping("/getActiveTeachByMajorGradeId")
    @ApiOperation("获取年级专业教学活动")
    public ResponseObject<List<ActiveTeach>> getActiveTeachByMajorGradeId(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                          @ApiParam(value = "对应年级专业id,全校传0", required = true) @RequestBody List<Long> majorGradeIds) {
        return ResponseObject.ofSuccess(ruleWeekDayService.getActiveTeachByMajorGradeId(taskId, majorGradeIds));
    }

    @PostMapping("/addActiveTeach")
    @ApiOperation("添加教学活动安排")
    public ResponseObject<Boolean> addActiveTeach(@ApiParam(value = "活动安排实体类", required = true) @RequestBody List<ActiveTeach> activeTeach,
                                                  @ApiParam(value = "任务流程 id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(ruleWeekDayService.addActiveTeach(activeTeach, taskId));
    }

    @DeleteMapping("/removeActiveTeach")
    @ApiOperation("删除教学活动")
    public ResponseObject<Boolean> addActiveTeach(@ApiParam(value = "教学活动id", required = true) @RequestParam Integer activeId) {
        return ResponseObject.ofSuccess(activeTeachService.removeById(activeId));
    }

    @GetMapping("/getHoliday")
    @ApiOperation("获取节假日安排")
    public ResponseObject<List<Holiday>> getHoliday(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        List<Holiday> holidays = holidayService.lambdaQuery().eq(Holiday::getTaskId, taskId).list();
        holidays.forEach(a -> {
//            a.setHolidayName(a.getHolidayName().replace("节", ""));
            a.setHolidayName(a.getHolidayName().replace("前", ""));
            a.setHolidayName(a.getHolidayName().replace("后", ""));
        });
        return ResponseObject.ofSuccess(holidays);
    }

    @PostMapping("/addHoliday")
    @ApiOperation("添加节假日安排")
    public ResponseObject<Holiday> addActiveTeach(@ApiParam(value = "节假日安排实体类", required = true) @RequestBody Holiday holiday) {
        holidayService.save(holiday);
        return ResponseObject.ofSuccess(holiday);
    }

    @DeleteMapping("/removeHoliday")
    @ApiOperation("删除节假日安排")
    public ResponseObject<Boolean> removeHoliday(@ApiParam(value = "节假日安排id", required = true) @RequestParam Integer holidayId) {
        return ResponseObject.ofSuccess(holidayService.removeById(holidayId));
    }

    @GetMapping("/syncHoliday")
    @ApiOperation("同步国家法定安排")
    public ResponseObject<List<Holiday>> syncHoliday(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(holidayService.syncHoliday(taskId));
    }

    @GetMapping("/getRoomList")
    @ApiOperation("获取教室列表")
    public ResponseObject<PageInfo<Room>> getRoomList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                      @ApiParam(value = "所属校区id") @RequestParam(required = false) Integer campusId,
                                                      @ApiParam(value = "对应场地类型id") @RequestParam(required = false) Integer roomType,
                                                      @ApiParam(value = "是否可用 0未启用 1启用") @RequestParam(required = false) Integer isEnable,
                                                      @ApiParam(value = "keyword") @RequestParam(required = false) String keyword,
                                                      @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                      @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize,
                                                      @ApiParam(value = "是否设置 2未设置") @RequestParam(required = false) Integer ruleSet,
                                                      @ApiParam(value = "楼宇") @RequestParam(required = false) Integer buildingId) {
        Page<Room> page = new Page<>(currentPage, pageSize);
        return ResponseObject.ofSuccess(roomService.getRoomList1(page, taskId, campusId, roomType, isEnable, ruleSet, keyword, buildingId));
    }

    @GetMapping("/getRoomTree")
    @ApiOperation("根据楼宇分组获取教室")
    public ResponseObject<List<RoomTreeVo>> getRoomTree(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                        @ApiParam(value = "教室名模糊查询") @RequestParam(required = false) String keyword) {
        return ResponseObject.ofSuccess(roomService.getRoomTree(taskId, keyword));
    }

    @PostMapping("/updateRoom")
    @ApiOperation("更新教室")
    public ResponseObject<Boolean> updateRoom(@ApiParam(value = "对应场地类型id") @RequestParam(required = false) Integer roomType,
                                              @ApiParam(value = "对应场地类型name") @RequestParam(required = false) String roomTypeName,
                                              @ApiParam(value = "场地容量") @RequestParam(required = false) Integer roomNum,
                                              @ApiParam(value = "是否可用 0未启用 1启用") @RequestParam(required = false) Integer isEnable,
                                              @ApiParam(value = "同时上课班级数") @RequestParam(required = false) Integer concurrentClass,
                                              @ApiParam(value = "备注") @RequestParam(required = false) String remark,
                                              @ApiParam(value = "教室id", required = true) @RequestBody List<Long> roomIds) {
        return ResponseObject.ofSuccess(roomService.updateRoom(roomType, roomTypeName, roomNum, isEnable, concurrentClass, remark, roomIds));
    }

    @PostMapping("/updateRoomPeriod")
    @ApiOperation("更新教室")
    public ResponseObject<Boolean> updateRoomPeriod(@ApiParam(value = "对应场地类型id", required = true) @RequestParam String baseSchemeId,
                                                    @ApiParam(value = "对应场地类型name", required = true) @RequestParam String baseSchemeName,
                                                    @ApiParam(value = "教室id", required = true) @RequestBody List<Room> roomIds,
                                                    @ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(roomService.updateRoomPeriod(baseSchemeId, baseSchemeName, roomIds, taskId));
    }

    @PostMapping("/clearRoom")
    @ApiOperation("清空教室设置")
    public ResponseObject<Boolean> clearRoom(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(roomService.clearRoom(taskId));
    }

    @PostMapping("/clearTerm")
    @ApiOperation("清空课时安排")
    public ResponseObject<Boolean> clearTerm(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {

        return ResponseObject.ofSuccess(ruleWeekDayService.clearTerm(taskId));
    }

    @GetMapping("/getCollegeRoom")
    @ApiOperation("获取学院场地分配列表")
    public ResponseObject<List<CollegeRoomVo>> getCollegeRoom(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                              @ApiParam(value = "学院id") @RequestParam(required = false) String baseCollegeId,
                                                              @ApiParam(value = "场地分配状态  0、未分配 1、已分配") @RequestParam(required = false) Integer roomSet) {
        return ResponseObject.ofSuccess(collegeService.getCollegeRoom(taskId, baseCollegeId, roomSet));
    }

    @PostMapping("/setCollegeRoom")
    @ApiOperation("设置学院场地分配")
    public ResponseObject<Boolean> setCollegeRoom(@ApiParam(value = "设置场地实体类", required = true) @Valid @RequestBody CollegeRoomInput collegeRoomInput) {
        return ResponseObject.ofSuccess(collegeService.setCollegeRoom(collegeRoomInput));
    }

    @PostMapping("/clearCollegeRoom")
    @ApiOperation("清空学院场地分配设置")
    public ResponseObject<Boolean> clearCollegeRoom(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(collegeService.clearCollegeRoom(taskId));
    }

    @GetMapping("/getMajorGrade")
    @ApiOperation("获取学生信息设置列表")
    public ResponseObject<PageInfo<MajorGrade>> getMajorGrade(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                              @ApiParam(value = "所属校区id") @RequestParam(required = false) Integer campusId,
                                                              @ApiParam(value = "基础平台学院id") @RequestParam(required = false) String baseCollegeId,
                                                              @ApiParam(value = "基础平台专业id") @RequestParam(required = false) String baseMajorId,
                                                              @ApiParam(value = "对应年级id") @RequestParam(required = false) String gradeId,
                                                              @ApiParam(value = "专业名模糊查询") @RequestParam(required = false) String keyword,
                                                              @ApiParam(value = "是否允许跨校区排课 0未开启 1已开启") @RequestParam(required = false) Integer isCampusCross,
                                                              @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                              @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<MajorGrade> majorGradePage = majorGradeService.lambdaQuery()
                .eq(MajorGrade::getTaskId, taskId)
                .eq(campusId != null, MajorGrade::getCampusId, campusId)
                .eq(StrUtil.isNotBlank(baseCollegeId), MajorGrade::getBaseCollegeId, baseCollegeId)
                .eq(StrUtil.isNotBlank(baseMajorId), MajorGrade::getBaseMajorId, baseMajorId)
                .eq(StrUtil.isNotBlank(gradeId), MajorGrade::getGradeId, gradeId)
                .like(StrUtil.isNotBlank(keyword), MajorGrade::getMajorName, keyword)
                .eq(isCampusCross != null && isCampusCross != 0, MajorGrade::getIsCampusCross, isCampusCross)
                .page(new Page<>(currentPage, pageSize));
        List<Student> students = studentService.lambdaQuery().eq(Student::getTaskId, taskId).list();
        for (MajorGrade majorGrade : majorGradePage.getRecords()) {
            long count = students.stream()
                    .filter(a -> a.getGradeId().equals(majorGrade.getGradeId()) && a.getMajorId().equals(majorGrade.getBaseMajorId()) && a.getBaseCollegeId().equals(majorGrade.getBaseCollegeId()))
                    .count();
            majorGrade.setStudentNum(count);
        }
        return ResponseObject.ofSuccess(PagingUtil.formatPageResult(majorGradePage));
    }


    @PostMapping("/setMajorGrade")
    @ApiOperation("批量修改学生信息设置")
    public ResponseObject<Boolean> setMajorGrade(@ApiParam(value = "设置年级专业实体类", required = true) @Valid @RequestBody MajorGradeInput majorGradeInput) {
        return ResponseObject.ofSuccess(majorGradeService.setMajorGrade(majorGradeInput));
    }

    @PostMapping("/clearMajorGrade")
    @ApiOperation("清空学生信息设置")
    public ResponseObject<Boolean> clearMajorGrade(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(majorGradeService.clearMajorGrade(taskId));
    }

    @GetMapping("/getTeacherCourseList")
    @ApiOperation("获取教师信息设置列表")
    public ResponseObject<PageInfo<TeacherCourseVo>> getTeacherCourseList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                          @ApiParam(value = "所属校区id") @RequestParam(required = false) Integer campusId,
                                                                          @ApiParam(value = "基础平台学院id") @RequestParam(required = false) String baseCollegeId,
                                                                          @ApiParam(value = "1、任课教师 2、助教教师") @RequestParam(required = false) Integer identity,
                                                                          @ApiParam(value = "是否允许跨校区排课 0未开启 1已开启") @RequestParam(required = false) Integer isCampusCross,
                                                                          @ApiParam(value = "教师名模糊查询") @RequestParam(required = false) String keyword,
                                                                          @ApiParam(value = "课程id") @RequestParam(required = false) Long courseId,
                                                                          @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                          @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {

        Page<TeacherCourseVo> page = new Page<>(currentPage, pageSize);
        return ResponseObject.ofSuccess(teacherService.getTeacherCourseList(page, taskId, campusId, baseCollegeId, identity, isCampusCross, keyword, courseId));
    }

    @PostMapping("/copyTermSetting")
    @ApiOperation("复制同学期设置")
    public ResponseObject<String> copyTermSetting(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                  @ApiParam(value = "0课时方案 1教学活动 2场地信息 3场地分配 4学生信息 5教师信息 6开课变更 7预选课", required = true) @RequestParam Integer flag) {
        return ResponseObject.ofSuccess(teacherService.copyTermSetting(taskId, flag));
    }

    @PostMapping("/setTeacherCourseList")
    @ApiOperation("批量设置教师信息")
    public ResponseObject<Boolean> setTeacherCourseList(@ApiParam(value = "设置教师信息实体类", required = true) @Valid @RequestBody TeacherCourseInput teacherCourseInput) {
        return ResponseObject.ofSuccess(teacherService.setTeacherCourseList(teacherCourseInput));
    }

    @PostMapping("/clearTeacherCourseList")
    @ApiOperation("清空教师信息设置")
    public ResponseObject<Boolean> clearTeacherCourseList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(teacherService.clearTeacherCourseList(taskId));
    }

    @GetMapping("/getRoomTypeList")
    @ApiOperation("获取场地类型")
    public ResponseObject<List<RoomTypeVo>> getRoomTypeList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(roomTypeService.getRoomTypeList(taskId));
    }

    @GetMapping("/updateRoomType")
    @ApiOperation("更新场地类型")
    public ResponseObject<Boolean> updateRoomType(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                  @ApiParam(value = "场地类型id(删除操作传该参数） ", required = false) @RequestParam(required = false) Long id,
                                                  @ApiParam(value = "场地类型名称（添加操作传该参数）", required = false) @RequestParam(required = false) String roomName) {
        return ResponseObject.ofSuccess(roomTypeService.updateRoomType(taskId, id, roomName));
    }
}
